package pos.data;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.*;
import pos.domain.Regalo;

public class JDBCRegaloDAO implements IRegaloDAO{

	Connection conn = ConnectionManager.getInstance().checkOut();

	@Override
	public Regalo select(String nombreRegalo) {
		
		PreparedStatement stmt = null;
		ResultSet result = null;
		Regalo r = null;
		String nombre ="%"+nombreRegalo+"%";
		nombre = nombre.replace(" ", "%");
		String sql = "SELECT * FROM regalo WHERE nombre like ?";

		try {
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, nombre);
			result = stmt.executeQuery();

			if(result.next()){
				r = new Regalo();
				r.setNombre(result.getString("nombre"));
				r.setIdRegalo(result.getInt("idRegalo"));
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return r;
	}
	
	
}
